home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / tool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-30  |  12.7 KB  |  449 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. /*  Tool.c  */
  22.  
  23. #include <externs.h>
  24.  
  25.  
  26. char avl_from_which_file[AVL_MAX_NAME_SIZE + 1];
  27.  
  28. #define AVL_TOOL_MNU_ITEMS  6
  29.  
  30.  
  31. int AVL_MOUNT_TOOL(char *local);
  32.  
  33. void AVL_TOOL_REFER_PCK(AVL_T_TOOL_PTR t)
  34. {
  35.     char this[AVL_MAX_LINEL + 1];
  36.     short i;
  37.     sprintf(this,"WITH %s;", t -> more[avl_cur_tool].with);
  38.     for(i = 0; i < strlen(this); ++i)
  39.         AVL_EDIT_INSERT(this[i]);
  40.     AVL_CURSOR_END();
  41.     AVL_EDIT_ENTER();
  42. }
  43.  
  44. void AVL_TOOL_REFER_PCK_USE(AVL_T_TOOL_PTR t)
  45. {
  46.     char this[AVL_MAX_LINEL + 1];
  47.     short i;
  48.     sprintf(this,"WITH %s; USE %s;"
  49.         , t -> more[avl_cur_tool].with
  50.         , t -> more[avl_cur_tool].with);
  51.     for(i = 0; i < strlen(this); ++i)
  52.         AVL_EDIT_INSERT(this[i]);
  53.     AVL_CURSOR_END();
  54.     AVL_EDIT_ENTER();
  55. }
  56.  
  57. void AVL_TOOL_INSERT_CODE(AVL_T_TOOL_PTR t)
  58. {
  59.     if (AVL_MOUNT_TOOL(t -> menu[avl_cur_tool].source));
  60. }
  61.  
  62. void AVL_TOOL_IDENT(int l)
  63. {
  64.     int i;
  65.     for(i = 0; i < l; ++i)
  66.         AVL_EDIT_INSERT(' ');
  67. }
  68.  
  69. static short avl_tool_in_error = 0;
  70.  
  71. static char avl_tool_new_package[AVL_MAX_LINEL + 1];
  72.  
  73. void AVL_TOOL_INSTANTIATE_PCK(AVL_T_TOOL_PTR t)
  74. {
  75.     char *msg = " Enter the New Package Name ";
  76.     char gen_pars[AVL_MAX_GEN_PARS][AVL_MAX_LINEL + 1];
  77.     char msg2[300];
  78.     AVL_WIN_PTR m = NULL;
  79.     char this[AVL_MAX_LINEL + 1];
  80.     char this2[AVL_MAX_LINEL + 1];
  81.     short i, j;
  82.     int where, where2;
  83.     avl_tool_in_error = 1;    
  84.     where = avl_windows[avl_window].txt_col;
  85.     m = AVL_MAKE_WINDOW(msg,7,4,9,5+62,avl_wnd_bk_color,avl_wnd_color);
  86.     if (AVL_PROMPT(1,1,this2,60)) {
  87.         AVL_DEL_WINDOW(m);
  88.         return;
  89.         }
  90.     AVL_DEL_WINDOW(m);
  91.     sprintf(avl_tool_new_package," USE %s;", this2);
  92.     sprintf(msg2," Parameter Passing for Package %s ", this2);
  93.     msg2[72] = '\0';
  94.     m = AVL_MAKE_WINDOW(msg2,4,3,9+t -> more[avl_cur_tool].n_pars,5+73,avl_wnd_bk_color,avl_wnd_color);
  95.     for(i = 0; i < t -> more[avl_cur_tool].n_pars; ++i)  {
  96. again:
  97.         _settextposition(i+1,1);
  98.         sprintf(msg2," Enter %d parameter (%s)\n",i+1,t -> more[avl_cur_tool].gen_pars[i]);
  99.         msg2[72] = '\0';
  100.         _outtext(msg2);
  101.         gen_pars[i][0] = '\0';
  102.         if (AVL_PROMPT(i+2,3,gen_pars[i],60)) {
  103.             AVL_DEL_WINDOW(m);
  104.             return;
  105.             }
  106.         sprintf(msg2,"Is parameter no. %d ok (Y/N) ?",i+1);
  107.         if (AVL_QUESTION(msg2) == 'N') goto again;
  108.         _settextposition(i+1,1);
  109.         t -> more[avl_cur_tool].gen_pars[i][0] = toupper(t -> more[avl_cur_tool].gen_pars[i][0]);
  110.         sprintf(msg2," %d %s: %-69s"
  111.             , i+1
  112.             , t -> more[avl_cur_tool].gen_pars[i]
  113.             , gen_pars[i]);
  114.         msg2[72] = '\0';
  115.         _outtext(msg2);
  116.         _settextposition(i+2,1);
  117.         sprintf(msg2,"%-72s"," ");
  118.         _outtext(msg2);
  119.         }
  120.     AVL_DEL_WINDOW(m);
  121.     sprintf(this,"WITH %s;", t -> more[avl_cur_tool].with);
  122.     AVL_TOOL_IDENT(where);
  123.     for(i = 0; i < strlen(this); ++i)
  124.         AVL_EDIT_INSERT(this[i]);
  125.     AVL_CURSOR_END();
  126.     AVL_EDIT_ENTER();
  127.     AVL_TOOL_IDENT(where);
  128.     sprintf(this,"PACKAGE %s IS NEW %s(", this2, t -> more[avl_cur_tool].pname);
  129.     for(i = 0; i < strlen(this); ++i)
  130.         AVL_EDIT_INSERT(this[i]);
  131.     where2 = avl_windows[avl_window].txt_col;
  132.     for(i = 0; i < t -> more[avl_cur_tool].n_pars; ++i)  {
  133.         if (i != 0)  {
  134.             avl_windows[avl_window].txt_col = 0;
  135.             avl_windows[avl_window].current_line -> line[0] = '\0';
  136.             AVL_TOOL_IDENT(where2-1);
  137.             AVL_EDIT_INSERT(',');
  138.             }
  139.         for(j = 0; j < strlen(gen_pars[i]); ++j)
  140.             AVL_EDIT_INSERT(gen_pars[i][j]);
  141.         if ((i + 1) == t -> more[avl_cur_tool].n_pars) {
  142.             AVL_EDIT_INSERT(')');
  143.             AVL_EDIT_INSERT(';');
  144.             }
  145.         else {
  146.             AVL_CURSOR_END();
  147.             AVL_EDIT_ENTER();
  148.             }
  149.         }
  150.     avl_tool_in_error = 0;    
  151. }
  152.  
  153.  
  154. void AVL_TOOL_INSTANTIATE_PCK_USE(AVL_T_TOOL_PTR t)
  155. {
  156.     int where, i;
  157.     where = avl_windows[avl_window].txt_col;
  158.     AVL_TOOL_INSTANTIATE_PCK(t);
  159.     if (avl_tool_in_error) 
  160.         return;
  161.     AVL_TOOL_IDENT(where);
  162.     for(i = 0; i < strlen(avl_tool_new_package); ++i)
  163.         AVL_EDIT_INSERT(avl_tool_new_package[i]);
  164.     AVL_CURSOR_END();
  165.     AVL_EDIT_ENTER();
  166. }
  167.  
  168. void AVL_TOOL_HELP(AVL_T_TOOL_PTR t)
  169. {
  170.     AVL_HELP(t -> more[avl_cur_tool].help);
  171. }
  172.  
  173.  
  174.  
  175. TOOL_SIZE avl_tool_mnu[AVL_TOOL_MNU_ITEMS] = 
  176.     {{"A Refer Package            ",AVL_TOOL_REFER_PCK}
  177.     ,{"B Refer Package & USE      ",AVL_TOOL_REFER_PCK_USE}
  178.     ,{"C Insert Source Code       ",AVL_TOOL_INSERT_CODE}
  179.     ,{"D Instantiate Package      ",AVL_TOOL_INSTANTIATE_PCK}
  180.     ,{"E Instantiate Package & USE",AVL_TOOL_INSTANTIATE_PCK_USE}
  181.     ,{"F Help                   "  , AVL_TOOL_HELP}
  182.     };
  183.     
  184.  
  185.  
  186. void AVL_GET_TOOLS(AVL_T_TOOL_PTR tool)
  187. {
  188.     FILE *fp, *fopen();
  189.     char s[200], s2[200];
  190.     char *pl;
  191.     int i, j, k, l;
  192.     tool -> no_menu = 0;
  193.     pl = getenv("ADAED");
  194.     if (pl == NULL)
  195.         pl=getenv("adaed");
  196.     if (pl != NULL)  
  197.         sprintf(s,"%s%c%s",pl,'\\',AVL_TOOL_FNAME);
  198.     else  {
  199.         AVL_ERROR("Set the ADAED environment variable first (see readme.gw).");
  200.         return;
  201.         }        
  202.     fp = fopen(s,"r");
  203.     if (fp == NULL)  {
  204.         sprintf(s2,"Can't access file \'%s\'", s);
  205.         AVL_ERROR(s2);
  206.         return;
  207.         }
  208.     while ( fgets(s2,200,fp) != NULL)  {
  209.         if (tool -> no_menu >= AVL_TOOL_MAX)  {
  210.             sprintf(s2,"Can't process more than %d tools. Rest ignored!", AVL_TOOL_MAX);
  211.             AVL_ERROR(s2);
  212.             break;
  213.             }
  214.         for(i = 0; s2[i] != '@' && s2[i] != '$' && i < AVL_TOOL_MAX_MENU; ++i)
  215.             tool -> menu[tool -> no_menu].menu_item[i] = s2[i];
  216.         tool -> menu[tool -> no_menu].menu_item[i] = '\0';
  217.         while (s2[i] != '$') ++i;
  218.         ++i;
  219.         for(j = 0; !((s2[i] == '\0') || (s2[i] == ' ') || (s2[i] == '\n')) && j < AVL_MAX_FILE_NAME; ++j, ++i)
  220.             tool -> menu[tool -> no_menu].source[j] = s2[i];
  221.         tool -> menu[tool -> no_menu].source[j] = '\0';
  222.         while (s2[i] != '$') ++i;
  223.         ++i;
  224.         for(j = 0; !((s2[i] == '\0') || (s2[i] == ' ') || (s2[i] == '\n')) && j < AVL_MAX_LINEL; ++j, ++i)
  225.             tool -> more[tool -> no_menu].help[j] = s2[i];
  226.         tool -> more[tool -> no_menu].help[j] = '\0';
  227.         while (s2[i] != '$') ++i;
  228.         ++i;
  229.         tool -> more[tool -> no_menu].type = s2[i];
  230.         /* Get the package name */
  231.         while (s2[i] != '$') ++i;
  232.         ++i;
  233.         for(j = 0; !((s2[i] == ' ') || (s2[i] == '\0') || (s2[i] == '\n')) && j < AVL_MAX_LINEL; ++j, ++i)
  234.             tool -> more[tool -> no_menu].with[j] = s2[i];
  235.         tool -> more[tool -> no_menu].with[j] = '\0';
  236.         if (tool -> more[tool -> no_menu].type == 'I')  {
  237.             /* Get the full package name  */
  238.             while (s2[i] != '$') ++i;
  239.             ++i;
  240.             for(j = 0; !((s2[i] == ' ') || (s2[i] == '\0') || (s2[i] == '\n')) && j < AVL_MAX_LINEL; ++j, ++i)
  241.                 tool -> more[tool -> no_menu].pname[j] = s2[i];
  242.             tool -> more[tool -> no_menu].pname[j] = '\0';
  243.             while (s2[i] != '$') ++i;
  244.             ++i;
  245.             /* Now get the number of parameters */
  246.             tool -> more[tool -> no_menu].n_pars = 0;
  247.             while(isdigit(s2[i])) 
  248.                 tool -> more[tool -> no_menu].n_pars = 
  249.                     tool -> more[tool -> no_menu].n_pars * 10 
  250.                                                          + (s2[i++] - '0');
  251.             /* Get an user message to prompt for each parameter */
  252.             for(k = 0; k < tool -> more[tool -> no_menu].n_pars && 
  253.                fgets(tool -> more[tool -> no_menu].gen_pars[k],200,fp) != NULL; 
  254.                ++k)  {
  255.                 l = strlen(tool -> more[tool -> no_menu].gen_pars[k]); 
  256.                 if (tool -> more[tool -> no_menu].gen_pars[k][l - 1] == '\n')
  257.                     tool -> more[tool -> no_menu].gen_pars[k][l - 1] = '\0'; 
  258.                 }
  259.             if (k != tool -> more[tool -> no_menu].n_pars)  {
  260.                 sprintf(s,"Invalid package parameter descriptor : %s", 
  261.                     tool -> menu[tool -> no_menu].menu_item);
  262.                 AVL_ERROR(s);
  263.                 sprintf(s,"Process suspended while working with item no. %d", tool -> no_menu);
  264.                 AVL_ERROR(s);
  265.                 fclose(fp);
  266.                 return;
  267.                 }
  268.             }                
  269.         tool -> no_menu += 1;
  270.         }
  271.     fclose(fp);
  272. }
  273.  
  274.  
  275. void AVL_SHOW_TOOL(AVL_T_TOOL_PTR w, int cols)
  276. {
  277.     short i, first, last, k;    
  278.     short co;
  279.     k = avl_cur_tool / 9;
  280.     first = k * 9;
  281.     last = first + 8;
  282.     if (last >= avl_size) 
  283.         last = avl_size - 1;
  284.     _settextposition(1,1);
  285.     _settextcursor(0x2000);  /*  turn cursor off */
  286.     _outtext("\n\n\n\n\n\n\n\n\n\n\n\n");
  287.     k = 1;
  288.     for(i = first; i <= last; ++i)  {
  289.         _settextposition(k,1);
  290.         if (i == avl_cur_tool)
  291.             co = _settextcolor(avl_men_ready);
  292.         else
  293.             co = _settextcolor(avl_men_letter);
  294.         _outtext(w -> menu[i].menu_item);
  295.         ++k;
  296.         }
  297.     _settextcolor(co);
  298. }
  299.                 
  300. int AVL_TOOL(AVL_T_TOOL_PTR w, int r)
  301. {
  302.         int x, i ;
  303.         int no = 0;
  304.         int ch, cols = 0, rows;
  305.         short co;
  306.         char msg[80];
  307.         AVL_WIN_PTR hw = NULL, hw2 = NULL, hw3 = NULL;
  308.         _settextcursor(0x2000);  /*  turn cursor off */
  309.         if (r < 10)
  310.             rows = r;
  311.         else 
  312.             rows = 9;
  313.         avl_size = r;
  314.         /* Compute the maximum window's length */
  315.         for(x = 0; x < r; ++x)  
  316.             if ((no = strlen(w -> menu[x].menu_item)) > cols)
  317.                 cols = no;
  318.  
  319.         cols += 2;
  320.         hw2 = AVL_MAKE_WINDOW("",15,47,15+9,47+29,avl_wnd_bk_color,avl_wnd_color);
  321.         _outtext(" Use the arrow keys to\n");
  322.         _outtext(" select a tool.\n");
  323.         _outtext("\n");
  324.         _outtext(" Press  the related action\n");
  325.         _outtext(" letter to cause the\n");
  326.         _outtext(" desired effect.\n");
  327.         _outtext("\n");
  328.         _outtext(" Press ESC to  cancel.");
  329.         hw3 = AVL_MAKE_WINDOW(" Actions "
  330.             ,3+rows+3,avl_menu[2].c
  331.             ,3+rows+3+AVL_TOOL_MNU_ITEMS+1,avl_menu[2].c+3+strlen(avl_tool_mnu[0].tool_opt+2)
  332.             ,avl_wnd_bk_color,avl_wnd_color);
  333.         for(i = 0; i < AVL_TOOL_MNU_ITEMS; ++i)  {
  334.             _settextposition(i+1,1);
  335.             co = _settextcolor(avl_men_ready);
  336.             sprintf(msg,"%c",avl_tool_mnu[i].tool_opt[0]);
  337.             _outtext(msg);
  338.             co = _settextcolor(avl_men_letter);
  339.             sprintf(msg,"%s", &avl_tool_mnu[i].tool_opt[1]);
  340.             _outtext(msg);
  341.             }
  342.         hw = AVL_MAKE_WINDOW(" Tool Box ",3,avl_menu[2].c,3+rows+1,avl_menu[2].c+cols+5,avl_wnd_bk_color,avl_wnd_color);
  343.  
  344.         while ( 1 )  {
  345.             AVL_SHOW_TOOL(w,cols-2);
  346.             ch = getch();
  347.             ch = toupper(ch);
  348.             if (ch >= 'A' && ch <= ('A' + AVL_TOOL_MNU_ITEMS - 1)) {
  349.                 if (ch == 'F')  {                
  350.                     avl_tool_mnu[ch - 'A'].proc(w);
  351.                     continue;
  352.                     }
  353.                 AVL_DEL_WINDOW(hw);
  354.                 AVL_DEL_WINDOW(hw3);
  355.                 AVL_DEL_WINDOW(hw2);
  356.                 avl_tool_mnu[ch - 'A'].proc(w);
  357.                 return 1;
  358.                 }
  359.             else {
  360.                 if (ch == 0 || ch == 0xE0) {
  361.                     ch = getch();
  362.                     switch( ch ) {
  363.                         case 72 : /* Up */ 
  364.                             if (--avl_cur_tool < 0)  
  365.                                 avl_cur_tool = avl_size - 1;
  366.                             break;
  367.                         case 80 : /* Down */ 
  368.                             if (++avl_cur_tool >= avl_size)
  369.                                 avl_cur_tool = 0;
  370.                             break;
  371.                         default : putchar(7); break;
  372.                         }
  373.                     continue;
  374.                     }
  375.                 if (ch == 27)  {
  376.                     AVL_DEL_WINDOW(hw);
  377.                     AVL_DEL_WINDOW(hw3);
  378.                     AVL_DEL_WINDOW(hw2);
  379.                     return -1;
  380.                     }
  381.                 putch(7);
  382.                 continue;
  383.                 }
  384.             }
  385. }            
  386.         
  387. int AVL_MOUNT_TOOL(char *local)
  388. {
  389.     AVL_EDIT_WINDOW_PTR w;
  390.     AVL_LINE_PTR temp, head = NULL;
  391.     AVL_WIN_PTR m1;
  392.     char fname[100];
  393.     int n1, n2, x;
  394.     char msg2[80];
  395.     char *pl;
  396.     pl = getenv("ADAED");
  397.     if (pl == NULL)
  398.         pl=getenv("adaed");
  399.     if (pl != NULL)  
  400.         sprintf(fname,"%s%c%s",pl,'\\',local);
  401.     else  {
  402.         AVL_ERROR("Set the ADAED environment variable first (see readme.gw).");
  403.         return;
  404.         }        
  405.  
  406.     w = &avl_windows[avl_window];
  407.     n1 = w -> txt_col;
  408.     avl_start_pos = AVL_COL() - 1;
  409.     if (avl_start_pos < 0) avl_start_pos = 0;
  410.     w -> txt_col = 0;
  411.     temp = AVL_MAKE_LINE("### DUMMY ###",0);
  412.     AVL_LINE_INSERT(temp,&head);
  413.     x = AVL_LOAD(fname, &head);
  414.     avl_start_pos = 0;
  415.  
  416.     if (!x) {
  417.         sprintf(msg2,"Can't open \'%s\'", fname);
  418.         AVL_ERROR(msg2);
  419.         w -> txt_col = n1;
  420.         return 0;
  421.         }
  422.     else {
  423.         AVL_CLEAR_BLOCK();
  424.         avl_block_first_line = head -> next;
  425.         avl_block_last_line = head -> previous;
  426.         avl_block_first_col = 0;
  427.         avl_block_last_col = strlen(head -> previous -> line) - 1;
  428.         if (avl_block_last_col < 0) avl_block_last_col = 0;
  429.         AVL_EDIT_COPY(1);
  430.         AVL_CLEAR_BLOCK();
  431.         return 1;
  432.         }
  433. }
  434.  
  435.  
  436. void AVL_CALL_TOOL()
  437. {
  438.     AVL_T_TOOL_SIZE t;
  439.     int n;
  440.     char s[100];
  441.     char this[200];
  442.     AVL_GET_TOOLS(&t);
  443.     n = AVL_TOOL(&t,t.no_menu);
  444.     if (n >= 0)  {
  445.         sprintf(s,"%s Inserted", t.menu[avl_cur_tool].menu_item);
  446.         strcpy(avl_message,s);
  447.         }
  448. }
  449.  
  450.